//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.Threading;
using Shp.Security.BrokeredReceiver.Properties;

namespace Shp.Security.BrokeredReceiver
{
    /// <summary>
    /// The configiration class for the SecurityTokenService.
    /// </summary>
    public class StsConfiguration : ConfigurationSection
    {
        const string SectionName = "securityTokenService";

        static StsConfiguration configuration = null;
        static object _configurationLock = new object();
        static int _recursionGuard = 0;

        /// <summary>
        /// Gets the current <see cref="StsConfiguration"/>.
        /// </summary>
        /// <value>The current <see cref="StsConfiguration"/>.</value>
        static public StsConfiguration Current
        {
            get
            {
                if (StsConfiguration.configuration == null)
                {
                    lock (_configurationLock)
                    {
                        if (StsConfiguration.configuration == null)
                        {
                            StsConfiguration configuration = null;
                            if (Interlocked.CompareExchange(ref _recursionGuard, 1, 0) > 0)
                            {
                                throw new ConfigurationErrorsException(Resources.ConfigurationNotInitialized);
                            }
                            try
                            {
                                if (configuration == null)
                                {
                                    configuration = (StsConfiguration)ConfigurationManager.GetSection(StsConfiguration.SectionName);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new ConfigurationErrorsException(ex.Message);
                            } 
                            if (configuration == null)
                            {
                                configuration = new StsConfiguration();
                            }
                            StsConfiguration.configuration = configuration;
                        }
                    }
                }
                return StsConfiguration.configuration;
            }
        }

        /// <summary>
        /// Gets a scoped certificate to be used for encrypting the SAML token when communicating back to the caller service.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Security.Cryptography.X509Certificates.X509Certificate2"></see> 
        /// to be used for encrypting the SAML token when communicating back to the caller service.
        /// </returns>
        [ConfigurationProperty("scopedCertificates")]
        public X509ScopedServiceCertificateElementCollection ScopedCertificates
        {
            get
            {
                return (X509ScopedServiceCertificateElementCollection)base["scopedCertificates"];
            }
        }
    }
}
